<template>
  <div>
    <el-card shadow="always" :style="{ margin: '20px' }">
      <el-form label-width="80px" :inline="true" size="normal">
        <el-form-item size="normal">
          <el-input
            placeholder="用户名"
            v-model="searchKey"
            size="small"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item :style="{ marginLeft: '20px' }" size="normal">
          <el-button
            type="primary"
            size="small"
            icon="el-icon-search"
            @click="goSearch()"
            >查询</el-button
          >
          <el-button size="small" type="infor" @click="goSearch('cl')"
            >清空</el-button
          >
        </el-form-item>
      </el-form>
      <el-button type="primary" size="small" @click="openAddroleItem"
        >添加</el-button
      >
      <el-button
        type="danger"
        size="small"
        @click="delMany"
        :disabled="checkTableList.length === 0"
        >批量删除</el-button
      >
      <el-table
        :data="tableitems"
        :style="{ marginTop: '20px' }"
        v-loading="loading"
        @selection-change="handleSelectionChange"
        border
        stripe
      >
        <el-table-column type="selection"> </el-table-column>
        <el-table-column type="index" label="序号"> </el-table-column>
        <el-table-column label="角色名称" prop="roleName">
          <template v-slot="{ row }">
            <el-input
              ref="WriteRef"
              v-model="row.roleName"
              size="small"
              v-if="row.isWrite"
              @blur="changeLook(row)"
              clearable
            ></el-input>
            <div v-else>{{ row.roleName }}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button
              type="info"
              size="mini"
              icon="el-icon-info"
              @click="setPermission(row)"
            ></el-button>
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-check"
              @click="renovate"
            ></el-button>
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="changeWrite(row)"
            ></el-button>
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="delOne(row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page.sync="parameter.page"
        :page-sizes="[3, 5, 7, 10]"
        :page-size.sync="parameter.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
      <!-- dialog -->
      <el-dialog title="添加角色" width="40%" :visible.sync="dialogFormVisible">
        <el-form :model="addSoleFrom">
          <el-form-item label="请输入新名称">
            <el-input
              v-model="addSoleFrom.name"
              ref="addSoleFromInput"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="saveRoleItem">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import {
  reqaclrole,
  saveRoleItem,
  reWriteRole,
  delRoleItem,
  delRoleList
} from "@/api/acl_role";
export default {
  name: "Role",

  data() {
    return {
      parameter: {
        page: 1,
        limit: 3
      },
      total: 0,
      tableitems: [],
      loading: false,
      //搜索的关键字
      searchKey: "",
      // dialog控制
      dialogFormVisible: false,
      //表单的数据控制
      addSoleFrom: {
        name: ""
      },
      // 判断角色名是否修改
      oldName: "",
      // 已经选中的表格数据
      checkTableList: []
    };
  },

  mounted() {
    this.getaclRole();
  },
  watch: {
    parameter: {
      deep: true,
      handler() {
        this.loading = true;
        this.getaclRole();
      }
    }
  },
  methods: {
    async getaclRole(keyWord) {
      let re = await reqaclrole(
        this.parameter.page,
        this.parameter.limit,
        keyWord
      );
      this.total = re.total;
      this.tableitems = re.items.map(item => {
        item.isWrite = false;
        return item;
      });
      this.loading = false;
    },
    goSearch(ifcl) {
      this.loading = true;
      if (ifcl) {
        this.getaclRole();
      } else {
        this.getaclRole(this.searchKey);
      }
    },
    openAddroleItem() {
      this.dialogFormVisible = true;
      this.addSoleFrom.name = "";
      this.$nextTick(() => {
        this.$refs.addSoleFromInput.focus();
      });
    },
    // 创建单个角色
    async saveRoleItem() {
      await saveRoleItem({ roleName: this.addSoleFrom.name });
      this.$message({
        type: "success",
        message: "数据添加成功"
      });
      this.dialogFormVisible = false;
      this.getaclRole();
    },
    //设置角色的权限
    setPermission(row) {
      const { id, roleName } = row;
      this.$emit("changeShow", false, { id, name: roleName });
    },
    // 展开修改角色名
    changeWrite(row) {
      row.isWrite = true;
      this.oldName = row.roleName;
      this.$nextTick(() => {
        this.$refs.WriteRef.focus();
      });
    },
    // 切换为观察模式
    async changeLook(row) {
      row.isWrite = false;
      if (this.oldName !== row.roleName) {
        await reWriteRole(row);
        this.$message({
          type: "success",
          message: "数据修改成功"
        });
        this.getaclRole();
      }
    },
    // 刷新数据
    renovate() {
      this.$message({
        type: "success",
        message: "数据刷新成功"
      });
      this.getaclRole();
    },
    //单个删除
    async delOne(row) {
      await delRoleItem(row.id);
    },
    //列表删除
    async delMany() {
      const idList = this.checkTableList;
      this.$confirm("是否确定删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        await delRoleList(idList);
        this.$message({
          type: "success",
          message: "数据删除成功"
        });
        this.getaclRole();
      });
    },
    // 选中列表时的回调
    handleSelectionChange(file) {
      this.checkTableList = file.map(item => item.id);
    }
  }
};
</script>

<style lang="scss" scoped></style>
